package com.afd.ozblip.activity;

import com.afd.ozblip.entity.UserEntity;
import com.afd.ozblip.image.LoadImage;
import com.afd.ozblip.util.Constants;
import com.afd.ozblip.util.StringUtil;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class PeopleDetailActivity extends BaseActivity {

	/****************** 视图变量 **************************/
	private ImageView userImageIv;
	private TextView userNameTv;
	private TextView userLevelTv;
	private TextView userPointTv;
	private TextView userSignatureTv;
	private LinearLayout collectLayout;
	private LinearLayout signLayout;
	private LinearLayout reviewLayout;
	private LinearLayout photoLayout;
	private Button addFriendBtn;
	/****************** 数据变量 **************************/
	private UserEntity userInfo;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.user_detail);
		// 启动返回功能
		toBack();
		// 寻找视图
		findViews();
		// 设置监听
		setListeners();
		// 获取参数
		getBundle();
		// 设置视图
		setViews();
	}

	/**
	 * 寻找视图
	 */
	public void findViews() {
		this.userImageIv = (ImageView) findViewById(R.id.iv_user_image);
		this.userNameTv = (TextView) findViewById(R.id.tv_user_name);
		this.userLevelTv = (TextView) findViewById(R.id.tv_user_level);
		this.userPointTv = (TextView) findViewById(R.id.tv_user_point);
		this.userSignatureTv = (TextView) findViewById(R.id.tv_user_signature);
		this.collectLayout = (LinearLayout) findViewById(R.id.ll_user_collect);
		this.signLayout = (LinearLayout) findViewById(R.id.ll_user_sign);
		this.reviewLayout = (LinearLayout) findViewById(R.id.ll_user_review);
		this.photoLayout = (LinearLayout) findViewById(R.id.ll_user_photo);
		this.addFriendBtn = (Button) findViewById(R.id.btn_user_toAddFriend);
	}

	/**
	 * 设置监听
	 */
	public void setListeners() {
		View.OnClickListener peopleDetailClick = new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				switch (v.getId()) {
				case R.id.btn_user_toAddFriend:
					if (userInfo == null) {
						showLoginDialog();
					} else {
						Intent addFriendIntent = new Intent(
								PeopleDetailActivity.this,
								AddFriendActivity.class);
						addFriendIntent
								.putExtra("userId", userInfo.getUserId());
						startActivity(addFriendIntent);
					}
					break;
				case R.id.ll_user_collect:
					Intent collectIntent = new Intent(
							PeopleDetailActivity.this,
							UserCollectActivity.class);
					collectIntent.putExtra("userId", userInfo.getUserId());
					startActivity(collectIntent);
					break;
				case R.id.ll_user_sign:
					Intent signedIntent = new Intent(PeopleDetailActivity.this,
							UserSignedActivity.class);
					signedIntent.putExtra("userId", userInfo.getUserId());
					startActivity(signedIntent);
					break;
				case R.id.ll_user_review:
					Intent reviewIntent = new Intent(PeopleDetailActivity.this,
							UserReviewListActivity.class);
					reviewIntent.putExtra("userId", userInfo.getUserId());
					startActivity(reviewIntent);
					break;
				case R.id.ll_user_photo:
					Intent photoIntent = new Intent(PeopleDetailActivity.this,
							UserPhotoActivity.class);
					photoIntent.putExtra("userId", userInfo.getUserId());
					startActivity(photoIntent);
					break;
				default:
					break;
				}
			}
		};
		this.addFriendBtn.setOnClickListener(peopleDetailClick);
		this.collectLayout.setOnClickListener(peopleDetailClick);
		this.signLayout.setOnClickListener(peopleDetailClick);
		this.reviewLayout.setOnClickListener(peopleDetailClick);
		this.photoLayout.setOnClickListener(peopleDetailClick);
	}

	/**
	 * 获取传递的参数
	 */
	private void getBundle() {
		Intent intent = getIntent();
		userInfo = (UserEntity) intent.getSerializableExtra("userInfo");
	}

	private void setViews() {
		if (userInfo != null) {
			String headUrl = userInfo.getUserHeadUrl();
			if (!StringUtil.isNullOrEmpty(headUrl)) {
				LoadImage li = new LoadImage(this);
				li.addTask(Constants.WEB_PATH + headUrl, this.userImageIv);
			}
			String nickName = userInfo.getUserNickName();
			if (!StringUtil.isNullOrEmpty(nickName)) {
				this.userNameTv.setText(nickName);
			}
			String userLevel = userInfo.getUserLevel();
			if (!StringUtil.isNullOrEmpty(userLevel)) {
				this.userLevelTv.setText(userLevel);
			}
			String userPoint = userInfo.getUserIntegral();
			if (!StringUtil.isNullOrEmpty(userPoint)) {
				this.userPointTv.setText(userPoint);
			}
			String userSignature = userInfo.getUserSignature();
			if (!StringUtil.isNullOrEmpty(userSignature)) {
				this.userSignatureTv.setText(userSignature);
			}
		}
	}
}